package com.example.emojibase.view;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.emojibase.EmojiBaseDB;
import com.example.emojibase.R;
import com.example.emojibase.adapter.EmojiAdapter;
import com.example.emojibase.pojo.Emoji;
import com.example.emojibase.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;


public class EmojiShow extends AppCompatActivity implements EmojiAdapter.OnItemClickListener{
    private final Context context=this;
    EmojiBaseDB emojiBaseDB;
    EmojiAdapter emojiAdapter;
    public static boolean isLogin=false;
    SQLiteDatabase db;

    private void getEmojisLocal(){
        List<Emoji>emojis=new ArrayList<>();
        Cursor cur=db.query("Emoji",new String[]{"emoji_id","name","avatar"},null,null,null,null,null);
        while(cur.moveToNext()){
            //将Blob数据转化为字节数组
            int emojiId=cur.getInt(cur.getColumnIndexOrThrow("emoji_id"));
            byte[] img=cur.getBlob(cur.getColumnIndexOrThrow("avatar"));
            String name=cur.getString(cur.getColumnIndexOrThrow("name"));
            Emoji emoji=new Emoji(emojiId, img,name);
            emojis.add(emoji);
        }
        emojiAdapter.setData(emojis);
        cur.close();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        emojiBaseDB=EmojiBaseDB.getInstance(this);
        setContentView(R.layout.emoji_show_local);
        Button button1=findViewById(R.id.button1);
        Button reset=findViewById(R.id.buttonReset);
        Button deleteAll=findViewById(R.id.buttonDeleteAll);
        RecyclerView recyclerView=findViewById(R.id.emojiRecyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        emojiAdapter=EmojiAdapter.getInstance(context);
        emojiAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(emojiAdapter);
        db=emojiBaseDB.getWritableDatabase();
        getEmojisLocal();

        button1.setOnClickListener(view -> {
            Intent intent;
            if (!isLogin)
                intent = new Intent(EmojiShow.this, Login.class);
            else
                intent = new Intent(EmojiShow.this, EmojiShowOnline.class);
            startActivity(intent);
        });

        deleteAll.setOnClickListener(view -> {
            String delete="DELETE FROM emoji";
            db.execSQL(delete);
            getEmojisLocal();
        });
        reset.setOnClickListener(view -> {
            String delete="DELETE FROM emoji";
            db.execSQL(delete);
            CommonUtils.bitmabToBytes(context,db);
            getEmojisLocal();
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getEmojisLocal();
    }

    @Override
    public void onDestroy(){
        db.close();
        emojiBaseDB.close();
        super.onDestroy();
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this, EmojiDetail.class);
        // 可以通过位置position传递数据到新页面
        intent.putExtra("ITEM_POSITION", position);
        startActivity(intent);
    }
}